//
//  PhotoTypeOfMemory.m
//  ChildHoodStemp
//
//  Created by zhuxuehang on 13-9-6.
//
//

#import <QuartzCore/QuartzCore.h>
#import "MicroDef.h"
#import "PhotoTypeOfMemory.h"
//#import "CommentList.h"
#import "UIUtil.h"
#import "Snsp.pb.h"
#import "UIUtil.h"
//#import "CHPostManager.h"
//#import "CHPostService.h"
//#import "ChildHoodPost.h"
#import "MutiltimediaView.h"
#import "MBProgressHUD.h"

#import "PublishCommentView.h"
#define PhotoHorSpace   10.0f
#define PhotoVerSpace   5.0f
#define ImageHeight     300.f
#define ImageOrigin_Y   ((iPhoneHeight-108.0)/2.0+ImageHeight/2)


@interface PhotoTypeOfMemory ()<UIScrollViewDelegate,UIActionSheetDelegate>
{
    int64_t _postId; //xuzuotao added
    id _service;    //xuzuotao added
//    ChildHoodPost* _post; //xuzuotao added
    
    UILabel *commentNum;
    UILabel *appreciateNum;
    UIView *_toolView;
}

@end

@implementation PhotoTypeOfMemory
@synthesize imgScrollView   = _imgScrollView;
@synthesize contentText     = _contentText;


//xuzutao added <<<<<<<<<<<<<<<
-(id)initWithPostId:(int64_t)postId andService:(id)service
{
    if(self = [super init])
    {
        _postId = postId;
        _service = service;
        
    }
    return self;
}
//xuzutao added >>>>>>>>>>

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    [CNMultiThemeManager registerForThemeChange:self];

    [self.view setBackgroundColor:RGBCOLOR(247, 243, 229)];
	[self initData];
    [self layoutDesign];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDelegatePost:) name:NOTIFY_MSG_MYPOSTS object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    NSLog(@"PhotoTypeOfMemory dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)initData
{
    
    _imgScrollView = [[UIScrollView alloc] init];
  //  _post = [CHPostManager loadMyPostByPostId:_postId];
  //      [self setTitle:[UIUtil getSystemTime:_post.createTime]];
    _contentText   = [[UILabel alloc] init];

}


- (UIView *)toolView
{
    
//    NSArray * arr = [_post praises];
//    [_post loadReplies];
    _toolView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, iPhoneHeight-108, HARDWARE_SCREEN_WIDTH, 44)];
//    _toolView.backgroundColor = [UIColor colorWithPatternImage:[CNMultiThemeManager imageForKey:@"tabbarview_background.png"]];
    _toolView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbarview_background.png"]];
    [_toolView setTag:10];
    
    CGFloat iconHeight = 18;
    CGFloat iconWidth  = 20;
    
    UIButton* appreciateBtn = [self buttonWithFrame:CGRectMake(3*PhotoHorSpace, 2*PhotoVerSpace, 50, 35)
                                    normalTitle:@"赞"
                                  selectedTitle:nil
                                         norImg:[UIImage imageNamed:@"mychildhood_praise.png"]
                                         selImg:nil
                                            tag:111
                                         action:@selector(appreciateTapPressed:)
                                   cornerRadius:15
                                                 bg:[UIImage imageNamed:@"mychildhood_praise_bg.png"]];
    
    UIButton* commentBtn = [self buttonWithFrame:CGRectMake(appreciateBtn.frame.origin.x + appreciateBtn.frame.size.width + 2*PhotoHorSpace, 2*PhotoVerSpace, 60, 35)
                                        normalTitle:@"评论"
                                      selectedTitle:nil
                                             norImg:[UIImage imageNamed:@"mychildhood_comment.png"]
                                             selImg:nil
                                                tag:112
                                             action:@selector(commentTapPressed:)
                                       cornerRadius:15
                                              bg:[UIImage imageNamed:@"mychildhood_praise_bg.png"]];
    
    //详情
    UIView *detailBtn = [[UIView alloc] initWithFrame:CGRectMake(320-3*PhotoHorSpace-110, 2*PhotoVerSpace, 90, 25)];
    [detailBtn setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"mychildhood_praise_bg.png"]]];
    [detailBtn.layer setCornerRadius:5];
    [detailBtn setClipsToBounds:YES];
    
    UIImageView *appreciateimageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(8,5, iconWidth, iconHeight)];
    [appreciateimageView2 setImage:[UIImage imageNamed:@"mychildhood_praise.png"]];
    [appreciateimageView2 setBackgroundColor:[UIColor clearColor]];
    
    appreciateNum = [[UILabel alloc] initWithFrame:CGRectMake(appreciateimageView2.frame.origin.x + appreciateimageView2.frame.size.width+2, appreciateimageView2.frame.origin.y-5, 25, 25)];
    [appreciateNum setFont:[UIFont systemFontOfSize:14]];
    [appreciateNum setBackgroundColor:[UIColor clearColor]];
//    if ([arr count]<1) {
//        [appreciateNum setText:@""];        //赞 的数量
//    }else
//    {
//        [appreciateNum setText:[NSString stringWithFormat:@"%d",[arr count]]];        //赞 的数量
//    }

    UIImageView *commentBtnImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(appreciateNum.frame.origin.x + appreciateNum.frame.size.width, appreciateimageView2.frame.origin.y, iconWidth, iconHeight)];
    [commentBtnImageView2 setImage:[UIImage imageNamed:@"mychildhood_comment.png"]];
    [commentBtnImageView2 setBackgroundColor:[UIColor clearColor]];
    
    commentNum = [[UILabel alloc] initWithFrame:CGRectMake(commentBtnImageView2.frame.origin.x + commentBtnImageView2.frame.size.width+2, commentBtnImageView2.frame.origin.y-5, 50, 25)];
    [commentNum setFont:[UIFont systemFontOfSize:14]];
    [commentNum setBackgroundColor:[UIColor clearColor]];
//    if (_post.commentCount<1) {
//        [commentNum setText:@""];        // 评论 的数量
//    }else
//    {
// //       [commentNum setText:[NSString stringWithFormat:@"%d",_post.commentCount]];        // 评论 的数量
//    }
    UITapGestureRecognizer *detailPressed = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailTapPressed:)];
    [detailBtn addGestureRecognizer:detailPressed];
    
    [detailBtn addSubview:appreciateimageView2];
    [detailBtn addSubview:appreciateNum];
    [detailBtn addSubview:commentBtnImageView2];
    [detailBtn addSubview:commentNum];

    [_toolView addSubview:appreciateBtn];
    [_toolView addSubview:commentBtn];
    [_toolView addSubview:detailBtn];
    
    return _toolView;
}

-(UIButton*)buttonWithFrame:(CGRect)frame normalTitle:(NSString*)nortitle selectedTitle:(NSString*)selTitle norImg:(UIImage*)norImg selImg:(UIImage*)selImg tag:(NSInteger)tagNum action:(SEL)action cornerRadius:(CGFloat)radius bg:(UIImage*)bgImg
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:frame];
    [btn setTitle:nortitle forState:UIControlStateNormal];
    [btn setTitle:selTitle forState:UIControlStateSelected];
    [btn setImage:norImg forState:UIControlStateNormal];
    [btn setImage:selImg forState:UIControlStateSelected];
    [btn setTag:tagNum];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [btn.layer setCornerRadius:radius];
    [btn setBackgroundImage:bgImg forState:UIControlStateNormal];
    [btn setClipsToBounds:NO];
    
    [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    return  btn;
}


- (void)layoutDesign
{
    UIButton *sharedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sharedBtn setFrame:CGRectMake(0, 0, 50, 30)];
    [sharedBtn addTarget:self action:@selector(sharedMethod:) forControlEvents:UIControlEventTouchUpInside];
    [sharedBtn setTitle:@"..." forState:UIControlStateNormal];
    [sharedBtn setBackgroundImage:nil forState:UIControlStateNormal];
    UIBarButtonItem *shareBar = [[UIBarButtonItem alloc] initWithCustomView:sharedBtn];
    [self.navigationItem setRightBarButtonItem:shareBar];
    

    [_imgScrollView setFrame:CGRectMake(0, 0, 320, ImageHeight)];
    [_imgScrollView setScrollEnabled:YES];
    [_imgScrollView setShowsHorizontalScrollIndicator:YES];
    [_imgScrollView setPagingEnabled:YES];
    [_imgScrollView setDelegate:self];
    [_imgScrollView setBounces:NO];
//    if(_post.materialCount > 0)
//    {
//    [_imgScrollView setContentSize:CGSizeMake(320*_post.materialCount, ImageHeight)];
//                    //印记图片部分
//    for (int i=0; i< _post.materialCount; i++) {
//        MutiltimediaView *view = [[MutiltimediaView alloc] initWithFrame:CGRectMake(320*i+20,40,280 , ImageHeight)];
//        SNSPMultimedia* media = [_post.multimediasList objectAtIndex:i];
//        if(media.hasNextFileUri)
//            [view setMultimediaFileUri:media.nextFileUri  type:media.materialType];
//        else
//            [view setMultimediaFileUri:media.fileUri  type:media.materialType];
//
//        [_imgScrollView addSubview:view];
//    }
//    }
    [_contentText setFrame:CGRectMake(PhotoHorSpace, _imgScrollView.frame.origin.y + ImageHeight + PhotoVerSpace, 320 - 2*PhotoHorSpace, 50)];
    [_contentText setFont:[UIFont systemFontOfSize:12]];
    [_contentText setBackgroundColor:[UIColor clearColor]];
    [_contentText setNumberOfLines:0];
//    [_contentText setText:_post.content];                       //印记文字部分
    UIFont *font1 = [UIFont fontWithName:@"Arial" size:12];
    CGSize contentsize = [_contentText.text sizeWithFont:font1 constrainedToSize:CGSizeMake(320-2*PhotoHorSpace, 200) lineBreakMode:NSLineBreakByCharWrapping];
    contentsize.height+=16;
    contentsize.width+=16;
    CGRect textFrame = _contentText.frame;
    textFrame.origin.y = HARDWARE_SCREEN_HEIGHT-108-contentsize.height;
    textFrame.size   = contentsize;
    [_contentText setFrame:textFrame];
    
    CGRect scroFra = _imgScrollView.frame;
    scroFra.origin.y = 0;
    scroFra.size.height = HARDWARE_SCREEN_HEIGHT-108-contentsize.height;
    [_imgScrollView setFrame:scroFra];
    
    UIView *toolBarView = [self toolView];
    
    [self.view addSubview:_imgScrollView];
    [self.view addSubview:_contentText];
    [self.view addSubview:toolBarView];
}

- (void)sharedMethod:(id)sender
{
    UIActionSheet *shareAction = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"发送给朋友",@"保存到手机",@"删除", nil];
    [shareAction showInView:self.view];
}


- (void)appreciateTapPressed:(id)sender
{
    NSLog(@"appreciateTapPressed");
//    [(CHPostService*)_service addMyPostPraise:_postId];
    MBProgressHUD* progress = [[MBProgressHUD alloc] initWithView:self.view];
    [progress setAlpha:0.5];
    progress.removeFromSuperViewOnHide = YES;
    [progress setLabelText:@"正在发送"];
    [self.view addSubview:progress];
    [self.view bringSubviewToFront:progress];
    [progress show:YES];
}

- (void)commentTapPressed:(id)sender
{
    NSLog(@"commentTapPressed");
    PublishCommentView* pubComm = [[PublishCommentView alloc] initWithReplyPostId:_postId server:_service];
    [self.navigationController pushViewController:pubComm animated:YES];
}

- (void)detailTapPressed:(id)sender
{
//    CommentList *comment = [[CommentList alloc] initWithPostId:_postId andService:_service];
  //  [self.navigationController pushViewController:comment animated:YES];
}

#pragma mark-
#pragma mark -- actionsheet--

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
        if (buttonIndex == 0) {
            NSLog(@"发送给朋友,跳转到联系人列表");
            
        }else if(buttonIndex == 1){
            NSLog(@"保存到手机,需进行本地处理");
            
        }else if (buttonIndex == 2)
        {
            NSLog(@"删除");
     //       [_service deleteMyPost:_postId];
            MBProgressHUD* progress = [[MBProgressHUD alloc] initWithView:self.view];
            [progress setAlpha:0.5];
            progress.removeFromSuperViewOnHide = YES;
            [progress setLabelText:@"正在删除..."];
            [self.view addSubview:progress];
            [self.view bringSubviewToFront:progress];
            [progress show:YES];
//            [self.navigationController popViewControllerAnimated:YES];
        }else
        {
            [self dismissModalViewControllerAnimated:YES];
        }
}

- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
    [self dismissModalViewControllerAnimated:YES];
}

-(void)onDelegatePost:(NSNotification*)noti
{
//    //判断印记状态，如果是文字印记，返回上一级；如果是图片印记返回根
//    ChildHoodPost* post = noti.object;
//    if (post.postId == _post.postId && post.postSta == kPostDeleted) {
//        [UIView animateWithDuration:0.3 animations:^{
//            MBProgressHUD* progress = [[MBProgressHUD alloc] initWithView:self.view];
//            [progress setAlpha:0.5];
//            progress.removeFromSuperViewOnHide = YES;
//            [progress setLabelText:@"删除完成..."];
//            [self.view addSubview:progress];
//            [self.view bringSubviewToFront:progress];
//            [progress show:YES];
//        } completion:^(BOOL finished) {
//            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
//        }];
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//    if (post.postSta == kPostSendOK) {
//        [UIView animateWithDuration:0.3 animations:^{
//            MBProgressHUD* progress = [[MBProgressHUD alloc] initWithView:self.view];
//            [progress setAlpha:0.5];
//            progress.removeFromSuperViewOnHide = YES;
//            [progress setLabelText:@"发送成功"];
//            [self.view addSubview:progress];
//            [self.view bringSubviewToFront:progress];
//            [progress show:YES];
//        } completion:^(BOOL finished) {
//            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
//        }];
//        //修改数字
//        [self setNewPraiseNumber];
//        [self setNewCommentNumber];
//    }
}

-(void)setNewPraiseNumber
{
    if ([self getNewPraiseCount]>0) {
        [appreciateNum setText:[NSString stringWithFormat:@"%d",[self getNewPraiseCount]]];
    }
}

-(void)setNewCommentNumber
{
    if ([self getNewCommentCount]>0) {
        [commentNum setText:[NSString stringWithFormat:@"%d",[self getNewCommentCount]]];
    }
}

-(NSInteger)getNewPraiseCount
{
//    return [[_post praises] count];
    return 0;
}

-(NSInteger)getNewCommentCount
{
//    [_post loadReplies];
//    return _post.commentCount;
    return 0;
}

#pragma mark - 换肤
-(void)applyTheme{
    if (_toolView) {
        _toolView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbarview_background.png"]];
    }
}

@end
